package com.jxwifi.cloud.quickcleanserver.main;

import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.idroid.utils.TextDrawable;
import com.idroid.widget.CircleImageView;
import com.jxwifi.cloud.quickcleanserver.R;
import com.jxwifi.cloud.quickcleanserver.app.AccountMgr;
import com.jxwifi.cloud.quickcleanserver.app.StateUtils;
import com.jxwifi.cloud.quickcleanserver.app.UrlPool;
import com.jxwifi.cloud.quickcleanserver.bean.InfoBean;
import com.jxwifi.cloud.quickcleanserver.bean.SystemBean;
import com.jxwifi.cloud.quickcleanserver.mycenter.AuthenticationActivity;
import com.jxwifi.cloud.quickcleanserver.mycenter.CertificationListActivity;
import com.jxwifi.cloud.quickcleanserver.mycenter.MyBalanceActivity;
import com.jxwifi.cloud.quickcleanserver.mycenter.PersonalDataActivity;
import com.jxwifi.cloud.quickcleanserver.mycenter.PresentationActivity;
import com.jxwifi.cloud.quickcleanserver.mycenter.UserActivity;
import com.jxwifi.cloud.quickcleanserver.mycenter.WithdrawalActivity;
import com.jxwifi.cloud.quickcleanserver.mycenter.WorkingConditionActivity;
import com.jxwifi.cloud.quickcleanserver.okhttp.AuzHttp;
import com.jxwifi.cloud.quickcleanserver.okhttp.DataJson_Cb;
import com.jxwifi.cloud.quickcleanserver.okhttp.Params;
import com.jxwifi.cloud.quickcleanserver.order.MyOrderActivity;
import com.jxwifi.cloud.quickcleanserver.order.TextActivity;
import com.jxwifi.cloud.quickcleanserver.upgrade.UpgradeNotice;
import com.jxwifi.cloud.quickcleanserver.utils.LazyLoadFragment;
import com.jxwifi.cloud.quickcleanserver.utils.NotifySetting;
import com.jxwifi.cloud.quickcleanserver.utils.PasswordLogin;
import com.jxwifi.cloud.quickcleanserver.utils.ToastUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 个人中心
 * Created by Administrator on 2019/5/13.
 */

public class MyFragment extends LazyLoadFragment {

    @Bind(R.id.iv_head)
    CircleImageView mIvHead;//头像
    @Bind(R.id.tv_include_login_name)
    TextView mYvIncludeLoginName;//姓名
    @Bind(R.id.tv_include_login_fraction)
    TextView mTvIncludeLoginFraction;//分数
    @Bind(R.id.rb_shop_details_list)
    RatingBar mRbList;//星星显示
    @Bind(R.id.tv_include_is_login)
    TextView mTvIncludeIsLogin;//登陆/注册入口
    @Bind(R.id.tv_main_authentication_text)
    TextView mTvMainAuthenticationText;
    @Bind(R.id.img_main_authentication)
    ImageView mImgMainAuthentication;
    @Bind(R.id.tv_rest_bg)
    TextView mTvRestBg;

    /*我的订单与收入*/
    @Bind(R.id.tv_my_balance_price)
    TextView mTvMyBalancePrice;//我的余额
    @Bind(R.id.tv_my_withdrawal_price)
    TextView mTvMyWithdrawalPrice;//累计提现
    @Bind(R.id.tv_my_order_number)
    TextView mTvMyOrderNumber;//我的订单
    @Bind(R.id.tv_my_reward_price)
    TextView mTvMyRewardPrice;//奖励

    private String mCustomer;//客服電話

    private InfoBean mInfoBean;

    public int setContentView() {
        return R.layout.fragment_my;
    }

    @Override
    protected void lazyLoad() {
        if (AccountMgr.isLogin(mBaseContext)) {
            memberinfo();//更新用户信息
        }
    }

    @Override
    protected void stopLoad() {

    }

    public void initView() {
        mYvIncludeLoginName.setText(AccountMgr.getrealName(mBaseContext));
        if (StateUtils.wait_order.equals(AccountMgr.getacceptStatus(mBaseContext))) {
            mTvRestBg.setVisibility(View.GONE);
        } else if (StateUtils.close_order.equals(AccountMgr.getacceptStatus(mBaseContext))) {
            mTvRestBg.setVisibility(View.VISIBLE);
            mTvRestBg.getBackground().setAlpha(50);
        }
        if (!TextUtils.isEmpty(AccountMgr.getheadpic(mBaseContext))) {
            Glide.with(mBaseContext).load(AccountMgr.getheadpic(mBaseContext)).dontAnimate().into(mIvHead);
        }else{
            Glide.with(mBaseContext).load(R.mipmap.icon_default_head).dontAnimate().into(mIvHead);
        }
        mTvIncludeLoginFraction.setText(AccountMgr.getgrade(mBaseContext) + "分");
        mRbList.setRating((float) (AccountMgr.getgrade(mBaseContext) / 20));
        mTvMyBalancePrice.setText("" + AccountMgr.getamount(mBaseContext));
        mTvMyOrderNumber.setText("" + AccountMgr.getorderCounts(mBaseContext));

        if (StateUtils.YES.equals(AccountMgr.getIsAuth(mBaseContext))) {
            mTvMainAuthenticationText.setText("已认证");
            mImgMainAuthentication.setVisibility(View.VISIBLE);
        } else {
            mTvMainAuthenticationText.setText("未认证");
            mImgMainAuthentication.setVisibility(View.GONE);
        }
        TextDrawable.drawable(mBaseContext, mTvMainAuthenticationText, R.mipmap.icon_right_wither, 2);
        if (AccountMgr.isLogin(mBaseContext)){
            cumulative();//获取累计提现与累计奖励
        }
    }

    @OnClick({R.id.iv_head, R.id.rel_my_balance, R.id.rel_my_reward, R.id.rel_my_order, R.id.rel_my_withdrawal,
            R.id.rel_main_authentication, R.id.tv_main_me_classroom, R.id.tv_main_me_ranking_list, R.id.tv_main_me_invitation,
            R.id.tv_main_me_modify_phone, R.id.tv_main_me_customer_service, R.id.tv_main_me_online_examination, R.id.tv_main_me_work_state,
            R.id.tv_agreement})
    void clickCK(View v) {
        Intent intent = new Intent();
        Toast toast;
        switch (v.getId()) {
            //个人信息
            case R.id.iv_head:
                intent.setClass(mBaseContext, PersonalDataActivity.class);
                startActivity(intent);
                break;
            //认证入口
            case R.id.rel_main_authentication:
                intent.setClass(mBaseContext, CertificationListActivity.class);
                startActivity(intent);
                break;
            //我的余额
            case R.id.rel_my_balance:
                intent.setClass(mBaseContext, MyBalanceActivity.class);
                startActivity(intent);
                break;
            //提现入口
            case R.id.rel_my_withdrawal:
                intent.setClass(mBaseContext, PresentationActivity.class);
                startActivity(intent);
                break;
            //我的订单
            case R.id.rel_my_order:
                intent.setClass(mBaseContext, MyOrderActivity.class);
                startActivity(intent);
                break;
            //奖励
            case R.id.rel_my_reward:
                //测试类，用于研究嵌入小功能的类都可以在里面写
               /* intent.setClass(mBaseContext,TextActivity.class);
                startActivity(intent);*/
                //UpgradeNotice.UpgradeData(mBaseContext,TAG, 1,getActivity());//升级通知
                break;
            case R.id.tv_main_me_classroom:
                toast = Toast.makeText(mBaseContext, "功能有待开发，敬请期待", Toast.LENGTH_SHORT);
                ToastUtils.showMyToast(toast, 3000);
                break;
            case R.id.tv_main_me_ranking_list:
                toast = Toast.makeText(mBaseContext, "功能有待开发，敬请期待", Toast.LENGTH_SHORT);
                ToastUtils.showMyToast(toast, 3000);
                break;
            case R.id.tv_main_me_invitation:
                toast = Toast.makeText(mBaseContext, "功能有待开发，敬请期待", Toast.LENGTH_SHORT);
                ToastUtils.showMyToast(toast, 3000);
                break;
            case R.id.tv_main_me_modify_phone:
                toast = Toast.makeText(mBaseContext, "功能有待开发，敬请期待", Toast.LENGTH_SHORT);
                ToastUtils.showMyToast(toast, 3000);
                break;
            //联系客服
            case R.id.tv_main_me_customer_service:
                inittell();
                break;
            case R.id.tv_main_me_online_examination:
                toast = Toast.makeText(mBaseContext, "功能有待开发，敬请期待", Toast.LENGTH_SHORT);
                ToastUtils.showMyToast(toast, 3000);
                break;
            //工作状态
            case R.id.tv_main_me_work_state:
                if (StateUtils.ing_order.equals(AccountMgr.getacceptStatus(mBaseContext))) {
                    toast = Toast.makeText(mBaseContext, "工作中不可修改工作状态，请先完成工作", Toast.LENGTH_SHORT);
                    ToastUtils.showMyToast(toast, 3000);
                } else {
                    if (!StateUtils.YES.equals(AccountMgr.getIsAuth(mBaseContext))) {
                        //Toast.makeText(mBaseContext, "您未认证或未通过，请认证后在操作", Toast.LENGTH_SHORT).show();
                        intent.setClass(mBaseContext, AuthenticationActivity.class);
                        startActivity(intent);
                        return;
                    } else {
                        intent.setClass(mBaseContext, WorkingConditionActivity.class);
                        startActivity(intent);
                    }
                }
                break;
            //用户协议
            case R.id.tv_agreement:
                intent.setClass(mBaseContext, UserActivity.class);
                startActivity(intent);
                break;
        }
    }

    public void cumulative() {
        AuzHttp.get(UrlPool.GET_UCCLEANER_DETAIL, new Params(), new DataJson_Cb() {
            @Override
            public void onSuccess(int code, String data) {
                if (code == 0) {
                    JSONObject obj = JSON.parseObject(data);
                    String str = obj.getString("amount");
                    String str1 = obj.getString("reward");
                    mTvMyWithdrawalPrice.setText(str);
                    mTvMyRewardPrice.setText(str1);
                } else {
                    PasswordLogin.passwordlogin(mBaseContext, TAG, AccountMgr.gettell(mBaseContext), AccountMgr.getpassword(mBaseContext));
                    if (PasswordLogin.mIsLogin) {
                        cumulative();
                    }
                }
            }

            @Override
            public void onFailure(int errorCode, String msg) {
                Toast toast = Toast.makeText(mBaseContext, msg, Toast.LENGTH_SHORT);
                ToastUtils.showMyToast(toast, 3000);
            }
        }, TAG);
    }

    public void memberinfo() {
        AuzHttp.get(UrlPool.GET_UCCLEANER_INFO, new Params(), new DataJson_Cb() {
            @Override
            public void onSuccess(int code, String data) {
                if (code == 0) {//正常数据
                    mInfoBean = JSON.parseObject(data, InfoBean.class);

                    AccountMgr.putUser(mBaseContext, mInfoBean, false);
                    initView();
                } else {//请他情况做密码登录接口请求操作后再次请求接口
                    PasswordLogin.passwordlogin(mBaseContext, TAG, AccountMgr.gettell(mBaseContext), AccountMgr.getpassword(mBaseContext));
                    if (PasswordLogin.mIsLogin) {
                        memberinfo();
                    }
                }
            }

            @Override
            public void onFailure(int errorCode, String msg) {
                Toast toast = Toast.makeText(mBaseContext, msg, Toast.LENGTH_SHORT);
                ToastUtils.showMyToast(toast, 3000);
            }
        }, TAG);
    }

    public void inittell() {
        Params params = new Params();
        AuzHttp.get(UrlPool.SYSTEM_LIST, params, new DataJson_Cb() {
            @Override
            public void onSuccess(int code, String data) {
                if (code == 0) {//正常数据
                    List<SystemBean> msystemBean = JSON.parseArray(data, SystemBean.class);
                    for (int i = 0; i < msystemBean.size(); i++) {
                        if ("kjb_ke_tell".equals(msystemBean.get(i).getKeyName())) {
                            mCustomer = msystemBean.get(i).getKeyValue();
                            break;
                        }
                    }
                    if (!TextUtils.isEmpty(mCustomer)) {
                        Intent mTelintent = new Intent(Intent.ACTION_DIAL);
                        Uri Urldata = Uri.parse("tel:" + mCustomer.replace("\\", ""));
                        mTelintent.setData(Urldata);
                        startActivity(mTelintent);
                    }
                } else {//请他情况做密码登录接口请求操作后再次请求接口
                    PasswordLogin.passwordlogin(mBaseContext, TAG, AccountMgr.gettell(mBaseContext), AccountMgr.getpassword(mBaseContext));
                    if (PasswordLogin.mIsLogin) {
                        inittell();
                    }
                }
            }

            @Override
            public void onFailure(int errorCode, String msg) {
                Toast toast = Toast.makeText(mBaseContext, msg, Toast.LENGTH_SHORT);
                ToastUtils.showMyToast(toast, 3000);
            }
        }, TAG);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (AccountMgr.isLogin(mBaseContext)) {
            memberinfo();
        }
    }

}
